﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TextScript : MonoBehaviour
{
    public SpriteRenderer SpriteRender;

    private const float FadeOutTime = 1f;
    private bool bIsGrounded;
    private float mGroundedTime;
    private bool bIsBoarded;
    public bool IsWrongText
    {
        get;
        set;
    }
    public bool IsRecoreded
    {
        get;
        set;
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (!bIsGrounded && collision.gameObject.layer == PhysicsLayer.Floor)
        {
            bIsGrounded = true;
            mGroundedTime = Time.time;
            GameManager.GetInstance().TextDrop(this);
        }
        if (!bIsGrounded && collision.gameObject.layer == PhysicsLayer.Board)
        {
            bIsBoarded = true;
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (!bIsGrounded && collision.gameObject.layer == PhysicsLayer.Decoder) //碰到decoder
        {
            bIsGrounded = true;
            IsRecoreded = true;
            mGroundedTime = Time.time;
            if (IsWrongText)
            {
                GameManager.GetInstance().AddRecordedWrongText(this);
            }
        }
    }
    private void Update()
    {
        if (bIsGrounded)
        {
            float percent = (Time.time - mGroundedTime) / FadeOutTime;
            percent = Mathf.Clamp01(percent);
            SpriteRender.color = new Color(1f, 1f, 1f, 1 - percent);
            if (percent >= 1f)
            {
                gameObject.SetActive(false);
            }
        }
    }
    public bool IsBoarded()
    {
        return bIsBoarded;
    }
}
